package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.CharacterSixAdapter
import com.horoscope.astrology.zodiac.signs.free.adapter.CharacterTextListAdapter
import com.horoscope.astrology.zodiac.signs.free.adapter.CharacterTraitsAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.CharacterBean
import com.horoscope.astrology.zodiac.signs.free.bean.CharacterTextListBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.CharacterDecoration
import com.horoscope.astrology.zodiac.signs.free.utils.ConstellationUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig
import com.horoscope.astrology.zodiac.signs.free.utils.ad.NativeAdUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADNativeManager
import kotlinx.android.synthetic.main.activity_character.*
import kotlinx.android.synthetic.main.ad_layout.*
import kotlinx.android.synthetic.main.view_network_error.*

class CharacterActivity : BaseActivity() {

    var horoscope = ""

    lateinit var recyclerView1: RecyclerView
    lateinit var recyclerView2: RecyclerView
    lateinit var recyclerView3: RecyclerView

    lateinit var adapter1: CharacterSixAdapter
    lateinit var adapter2: CharacterTraitsAdapter
    lateinit var adapter3: CharacterTextListAdapter

    val bean = ArrayList<String>()
    var traitsList = ArrayList<CharacterTextListBean>()
    var textList = ArrayList<CharacterTextListBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_character)
        initView()
        activityShowAd("Interstitial_Character")
    }

    private fun initView() {
        showLoading()
        if (intent.getStringExtra("horoscope") != null) {
            horoscope = intent.getStringExtra("horoscope") ?:""
            iv_title_img.setImageResource(ConstellationUtil.getDrawableIdByName(horoscope))
        } else {
            horoscope = AppSettingManager.readObject("user_info")!!.constellationName
            iv_title_img.setImageResource(
                ConstellationUtil.getDrawableIdByName(
                    AppSettingManager.readObject(
                        "user_info"
                    )!!.constellationName
                )
            )
        }

        recyclerView1 = findViewById(R.id.character_six_rv)
        recyclerView2 = findViewById(R.id.character_traits)
        recyclerView3 = findViewById(R.id.character_text_list)

        getData()

        recyclerView1.layoutManager = GridLayoutManager(this, 2)
        recyclerView1.addItemDecoration(CharacterDecoration(15f, 15f))

        recyclerView2.layoutManager = LinearLayoutManager(this)

        recyclerView3.layoutManager = LinearLayoutManager(this)
        recyclerView3.addItemDecoration(CharacterDecoration(20f, 0f))

        iv_back.setOnClickListener {
            finish()
        }

        tv_network_ok.setOnClickListener {
            include_error.visibility = View.GONE
            showLoading()
            getData()
        }
    }

    private fun isShowNativeAd() {
        val nativeAdBean = ADNativeManager.mInstance.getNativeAdBean("Native_Character")
        if (nativeAdBean != null) {
            include_ad.visibility = View.VISIBLE
            // 通过advertiser判断显示那个原生广告视图
            when (nativeAdBean.adInfo.advertiser) {
                ADConfig.FB -> {
                    native_ad_container.visibility = View.VISIBLE
                    ad_frame.visibility = View.GONE
                    if (nativeAdBean.facebookNativeAD != null) {
                        NativeAdUtil.inflateFacebookAd(
                            nativeAdBean.facebookNativeAD,
                            include_ad, "Horoscope_Small", "Native_Character"
                        )
                        include_ad.visibility = View.VISIBLE
                    } else {
                        include_ad.visibility = View.GONE
                    }
                }
                ADConfig.ADMOB -> {
                    ad_frame.visibility = View.VISIBLE
                    native_ad_container.visibility = View.GONE
                    if (nativeAdBean.googleNativeAd != null) {
                        NativeAdUtil.inflateAdMobAd(
                            nativeAdBean.googleNativeAd!!,
                            include_ad,
                            "Horoscope_Small"
                        )
                        include_ad.visibility = View.VISIBLE
                    } else {
                        include_ad.visibility = View.GONE
                    }
                }
            }
        }
    }

    private fun getData() {
        bean.clear()
        traitsList.clear()
        textList.clear()
        NetworkEngine.getCharacter(horoscope, object : NetworkListener<CharacterBean>() {
            override fun onSuccess(t: CharacterBean) {

                bean.add(t.data.symbol)
                bean.add(t.data.color)
                bean.add(t.data.element)
                bean.add(t.data.duality)
                bean.add(t.data.quality)
                bean.add(t.data.most_compatible_with)
                adapter1 = CharacterSixAdapter(this@CharacterActivity, bean)
                recyclerView1.adapter = adapter1


                traitsList = t.data.traits
                val bean = CharacterTextListBean()
                bean.title = horoscope
                val adBean = CharacterTextListBean()
                adBean.ad = true
                traitsList.add(0, bean)
                traitsList.add(traitsList.size, adBean)
                adapter2 = CharacterTraitsAdapter(this@CharacterActivity, traitsList)
                recyclerView2.adapter = adapter2

                textList = t.data.text_list
                textList.add(CharacterTextListBean()) // share
                adapter3 = CharacterTextListAdapter(this@CharacterActivity, textList)
                recyclerView3.adapter = adapter3

                try {
                    isShowNativeAd()
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                showLoaded()

            }

            override fun onFailed(t: CharacterBean) {
                super.onFailed(t)
                showLoaded()
                errotMake()
            }

            override fun onError(e: String?) {
                super.onError(e)
                errotMake()
            }

        })
    }

    private fun errotMake() {
        showLoaded()
        include_error.visibility = View.VISIBLE
    }
}
